//
//  KXListViewController.swift
//  KXBook
//
//  Created by  on 2022/9/10.
//

import UIKit

class KXListViewController: KXViewController {
    @IBOutlet var typeSelectBtn: KXTypeSelectBtn!
    @IBOutlet var tableView: UITableView!
    @IBOutlet var collectionView: UICollectionView!
    var type:KXAddType = .pay
    var click=0
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = LS("明细")
        self.loadArray(self.type)
        self.typeSelectBtn.type = self.type
        weak var weakself = self
        self.typeSelectBtn.block = { type in
            self.type = type
            weakself?.loadArray(type)
        }
        self.tableView.register(KXHomeCell.nib(), forCellReuseIdentifier:KXHomeCell.ID())
        self.collectionView.register(KXAddTypeCell.nib(), forCellWithReuseIdentifier: KXAddTypeCell.ID())
    }
    
    func loadArray(_ type:KXAddType){
        self.type = type
        let whereStr = String(format:"where %@=%@",bg_sqlKey("isPay"),bg_sqlValue("\(type == .pay ? 1 : 0)"))
        KXTypeModel.bg_findAsync(nil, where: whereStr, complete: {array in
            DispatchQueue.main.async {
                self.array = array as? [KXTypeModel] ?? [KXTypeModel]()
                if self.array.count > 0{
                    self.loadrecord(0)
                }
            }
        })
    }
    
    func loadrecord(_ index:Int){
        self.click = index
        let model = self.array[index]
        let str = String(format: "where %@=%@ and %@ and %@", bg_sqlKey("userID"),bg_sqlValue(APP.user.id),bg_keyPathValues(["type.name",bg_equal,model.name]),bg_keyPathValues(["type.isPay",bg_equal,self.type == .pay ? 1 : 0]))
        KXRecord.bg_findAsync(nil, where: str as String) { success in
            DispatchQueue.main.async {
                self.record = success as! [KXRecord]
            }
        }
    }
    
    var array:[KXTypeModel] = [KXTypeModel](){
        didSet{
            self.collectionView.reloadData()
        }
    }
    var record = [KXRecord](){
        didSet{
            self.tableView.reloadData()
        }
    }
}


//MARK: - UITableViewDataSource
extension KXListViewController : UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.record.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: KXHomeCell.ID()) as! KXHomeCell
        cell.model = self.record[indexPath.row]
        return cell
    }
    
}
//MARK: - UITableViewDelegate
extension KXListViewController : UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = KXDetailController()
        vc.model = self.record[indexPath.row]
        self.push(vc, true)
    }
}


//MARK: - UICollectionViewDataSource
extension KXListViewController : UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.array.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KXAddTypeCell.ID(), for: indexPath) as! KXAddTypeCell
        let model = self.array[indexPath.row]
        cell.img.image = UIImage(named:model.icon)
        cell.type.text = LS(model.name)
        cell.bgImg.bgHexColor = self.click == indexPath.row ? "#E27BC1" : "#969696"
        return cell
    }
    
}
//MARK: - UICollectionViewDelegate
extension KXListViewController : UICollectionViewDelegate
{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.loadrecord(indexPath.row)
        collectionView.reloadData()
    }
}

//MARK: - UICollectionViewDelegateFlowLayout
extension KXListViewController : UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: 60, height: 80)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
    {
        return 20
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
    {
        return 20
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
    {
        return UIEdgeInsets(top:20, left: 20, bottom: 20, right: 20)
    }
}
